
export default {
  props: ['signlog','signedList'],
  data() {
    return {
      // ------------------------
      weekList: ['一', '二', '三', '四', '五', '六', '日'], // 周列表
      monthDayList: [], // 当月的日列表
      currentYear: '', // 当前年
      currentMonth: '', // 当前月
      currentDay: '', // 当前日
      today: '', // 今日
      selectedDate: '',
      formatSignedList: [], // 将签到日期格式化
      isDisabledNextMonth: true, // 是否能跳转下一个月
      // sign_status:false,
    };
  },
  watch:{
    sign_status(val){
      this.status = val;
    },
    signedList(val){
      this.formatSignedList = val;
    }
  },
  methods:{
    //---------------------------------------
    //判断是否为签到日期
    isSigned(date, list) {
      if (list.indexOf(date) > -1) {
        return true;
      }
      return false;
    },
    //对十号前面的加0
    formatZero(number) {
      // if (number < 10 && number > 0) {
      //   number = '0' + number;
      // }
      return number;
    },
    //初始化当前天数
    initCurrentDate(date) {
      this.currentDate = date || new Date();
      this.currentYear = this.currentDate.getFullYear();
      this.currentMonth = this.currentDate.getMonth() + 1;
      this.currentDay = this.currentDate.getDate();
      // 传的日期为空默认今日
      if (!date) {
        this.today = `${this.currentYear}/${this.currentMonth}/${this.currentDay}`;
        this.days = this.currentDay;
      }
      this.initMonthDayList();
    },
    //初始化具体日期
    initMonthDayList() {
      // 获取当月1号是周几
      let date = new Date(this.currentYear, this.currentMonth-1, 1);
      let weekDay = date.getDay() ;

      // 获取当月的总天数
      let monthFirstDay = new Date(this.currentYear, this.currentMonth, 0);
      let monthDay = monthFirstDay.getDate();

      let list = [];
      if(weekDay == 0){
        for (let index = 0; index < 6; index++) {
          list.push(null);
        }
      }else{
        for (let index = 0; index < weekDay - 1; index++) {
          list.push(null);
        }
      }
      for (let index = 1; index <= monthDay; index++) {

        if(this.currentMonth >=1 && this.currentMonth<10){
          if(index>=1 && index <10 ){
            const itemDate = `${this.currentYear}/0${this.currentMonth}/0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }else{
            const itemDate = `${this.currentYear}/0${this.currentMonth}/${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }
        }
        else if(this.currentMonth >=10){
          if(index>=1 && index <10 ){
            const itemDate = `${this.currentYear}/${this.currentMonth}/0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }else{
            const itemDate = `${this.currentYear}/${this.currentMonth}/${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}/${this.currentMonth}/${index}`).getTime()
            };
            list.push(item);
          }

        }
      }
      this.monthDayList = list;
    },
    //上一月
    handlePreMonth() {
      this.handleToggleMonth(-1);
      this.getnewDate();
      //
    },
    //下一月
    handleNextMonth() {
      this.handleToggleMonth(1);
      this.getnewDate();
    },
    // 切换月
    handleToggleMonth(data) {
      let month = this.currentMonth += data;
      let year = this.currentYear;
      if (month <= 0) {
        month = 12;
        year--;
      } else if (month > 12) {
        month = 1;
        year++;
      }
      this.currentYear=year,
      this.currentMonth=month;
      this.initMonthDayList();
      // this.triggerEvent('change', { date: `${year}/${month}` });
    },
    //点击签到 获取数据
    getSign() {
      $http
        .get(
          "plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index",
          {},
          "loading"
        )
        .then(res => {
          this.formatSignedList = res.data.sign_log;
          // this.sign_status = res.data.sign_status;
          this.fun.setWXTitle(this.sign_name || '签到');
        });
    },
    isLastDay(time){
      return  new Date().getTime()>time?true:false;
    },
    getnewDate() {
      let data = { month: this.currentMonth, year: this.currentYear };
      $http
        .post(
          'plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index',
          data,
          'loading'
        )
        .then(res => {
          this.formatSignedList = res.data.sign_log;
        });
    },
  },
  computed:{
  },
  created(){
    this.initCurrentDate();
    this.getSign();
  },
  mounted(){
  }

};